package org.dromara.xxmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.demo.base.ErrorCode;
import org.dromara.demo.common.XxmjException;
import org.dromara.xxmj.domain.XxmjUserExt;
import org.dromara.xxmj.domain.bo.XxmjUserExtBo;
import org.dromara.xxmj.domain.vo.XxmjUserExtVo;
import org.dromara.xxmj.mapper.XxmjUserExtMapper;
import org.dromara.xxmj.service.IXxmjUserExtService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 小巷密卷用户扩展Service业务层处理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class XxmjUserExtServiceImpl  implements IXxmjUserExtService {

    @Resource
    private XxmjUserExtMapper baseMapper;

    /**
     * 查询用户扩展信息
     *
     * @param id 主键
     * @return 用户扩展信息
     */
    @Override
    public XxmjUserExtVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 根据用户ID查询用户扩展信息
     *
     * @param userId 用户ID
     * @return 用户扩展信息
     */
    @Override
    public XxmjUserExtVo queryByUserId(Long userId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<XxmjUserExt>()
            .eq(XxmjUserExt::getUserId, userId));
    }

    /**
     * 根据手机号查询用户扩展信息
     *
     * @param phoneNumber 手机号
     * @return 用户扩展信息
     */
    @Override
    public XxmjUserExtVo queryByPhoneNumber(String phoneNumber) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<XxmjUserExt>()
            .eq(XxmjUserExt::getUserId, phoneNumber));
    }

    /**
     * 查询用户扩展列表
     *
     * @param bo 用户扩展信息
     * @return 用户扩展集合
     */
    @Override
    public List<XxmjUserExtVo> queryList(XxmjUserExtBo bo) {
        LambdaQueryWrapper<XxmjUserExt> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XxmjUserExt> buildQueryWrapper(XxmjUserExtBo bo) {
        LambdaQueryWrapper<XxmjUserExt> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, XxmjUserExt::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCategory()), XxmjUserExt::getUserCategory, bo.getUserCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneVerified()), XxmjUserExt::getPhoneVerified, bo.getPhoneVerified());
        return lqw;
    }

    /**
     * 新增用户扩展
     *
     * @param bo 用户扩展信息
     * @return 结果
     */
    @Override
    public void insertByBo(XxmjUserExtBo bo) {
        XxmjUserExt add = MapstructUtils.convert(bo, XxmjUserExt.class);
        validEntityBeforeSave(add);
        try {
            baseMapper.insert(add);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("新增用户扩展 baseMapper.insert .Error");
        }
//        baseMapper.insert(add);
//        if (flag) {
        if (add != null) {
            bo.setId(add.getId());
        }
//        }
//        return flag;
    }

    /**
     * 修改用户扩展
     *
     * @param bo 用户扩展信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(XxmjUserExtBo bo) {
        XxmjUserExt update = MapstructUtils.convert(bo, XxmjUserExt.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(XxmjUserExt entity) {
        // 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户扩展
     *
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
